package com.zidian.study.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zidian.study.bean.TaskWorkBean;

import java.util.List;

import com.zidian.student.trucks.R;

/**
 * Created by Administrator on 2016/8/24 0024.
 * 作业适配器
 */
public class TaskWorkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<TaskWorkBean.DataBean> data;
    private static boolean hasMore = true;


    public TaskWorkAdapter(Context context, List<TaskWorkBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }


    public void setMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return data.size() == 0 ? 0 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_task_work, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_interaction_ask_foot, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {

            ((ItemViewHolder) holder).className.setText(data.get(position).getWorkName());
            ((ItemViewHolder) holder).startTime.setText(data.get(position).getReleaseTime());
            ((ItemViewHolder) holder).endTime.setText(data.get(position).getHandOverTime());
            ((ItemViewHolder) holder).content.setText(data.get(position).getSchoolWorkContent());
            ((ItemViewHolder) holder).readNum.setText(data.get(position).getWorkType());


            if (onItemClickListener != null) {
            }
        } else if (holder instanceof FootViewHolder) {
            if (!hasMore) {
                ((FootViewHolder) holder).loadText.setText("全部数据加载完成!");
                ((FootViewHolder) holder).progressBar.setVisibility(View.GONE);
            } else {
                ((FootViewHolder) holder).loadText.setText("正在加载...");
                ((FootViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
            }

        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView className;
        TextView startTime;
        TextView endTime;
        TextView content;
        TextView readNum;

        public ItemViewHolder(View view) {
            super(view);

            className = (TextView) view.findViewById(R.id.work_class_name);
            startTime = (TextView) view.findViewById(R.id.work_begin_time);
            endTime = (TextView) view.findViewById(R.id.work_end_time);
            content = (TextView) view.findViewById(R.id.work_content);
            readNum = (TextView) view.findViewById(R.id.work_read_count);

        }
    }

    static class FootViewHolder extends RecyclerView.ViewHolder {
        TextView loadText;
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            loadText = (TextView) view.findViewById(R.id.load_text);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        }
    }


}
